<template>
	<view class="content">
		<view class="content-title" v-if="src">
		</view>
		<view class="myForm">
			<u-button type="warning" shape="circle" text="微信登录" @click="toWxLogin"></u-button>
		</view>
	</view>
</template>

<script>
	import store from '@/store'
	import {
		wxLogin
	} from '@/config/api.js'
	export default {
		data() {
			return {
				src: '/static/myLogo.png',
				pageUrl: ''
			}
		},
		onLoad(options) {
			this.pageUrl = options.pageUrl
			console.log(options.pageUrl)
		},
		methods: {
			toPasswordLogin() {
				uni.$u.route({
					url: '/pages/userCenter/userLogin',
					type: 'reLaunch'
				});
			},
			toWxLogin() {
				let _this = this
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log("微信login loginRes ==> ", loginRes)
						let data = {
							code: loginRes.code
						}
						wxLogin(data).then((res) => {
							if (res.code == 200) {
								uni.$u.toast("登录成功")
								store.commit('setUserInfo', res.data);
								if (res.data.isNew) {
									uni.$u.route({
										url: '/pages/userCenter/setWxUser',
										type: 'reLaunch'
									});
								} else {
									if (this.pageUrl) {
										uni.$u.route({
											url: this.pageUrl,
											type: 'reLaunch'
										});
									} else {
										uni.$u.route({
											url: '/pages/index/index',
											type: 'reLaunch'
										});
									}
								}
							} else {
								uni.$u.toast(res.msg)
							}
						})
					},
					fail: function(err) {
						// 登录授权失败  
						// err.code是错误码
						console.log("登录授权失败  ==>  ", err)
					}
				});
			}
		}
	}
</script>

<style scoped>
	.content {
		margin: auto;
		width: 80%;
		/* border: 3px solid green; */
		padding: 20px;
		align-items: center;
		justify-items: center;
	}

	.content-title {
		margin-top: 40px;
		justify-items: center;
		align-items: center;
		display: block;
		margin: auto;
		width: 20%;
	}

	.myForm {
		margin-top: 60px;
	}

	.myBtn {
		margin-top: 30px;
	}
</style>